import { treeInstructAddress } from "../../../service/api/api-instruct-address";

// pages/instruct/purple/index.ts
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentUser: {},//当前用户
    access: {} as any,// 当前用户权限
    initSaveTreeAddressList: [],//通讯录列表
    purples: '',//所选择的通讯录
    purplesIds: '',//所选择的通讯录的id
    nodeId: '',//根节点id
  },
  //新增通讯录
  handleCreatePurple() {
    wx.navigateTo({
      url: '/pages/instruct/address/editor/index'
    })
  },
  /**
  * 加载 保存 推送 单位
  */
  async loadTreePurpleList() {
    const response = await treeInstructAddress();
    if (response.code === 0) {
      this.setData({
        initSaveTreeAddressList: this.readyTreeAddressData(response.data)
      })
    }
  },
  /**
 * Tree View 指派通讯录 点击事件
 * @param e 
 */
  onClickDrawerAddressItem(e) {


  },
  onClickTreeViewAssignItemText(e) {
    const { item } = e.detail.item.currentTarget.dataset;
    var parentId = item.parentId;
    var id = item.id;
    if (parentId === "0") {
      wx.navigateTo({
        url: '/pages/instruct/address/editor/index?id=' + id
      })
      this.setData({
        nodeId: id
      })
    }
    //点击text 返回的id
  },
  /**
* 预备Tree结构数据
* @param data
*/
  readyTreeAddressData(data: Array<UserData.Organization>) {
    const treeData = [];

    if (data) {
      data.forEach((val: any, idx, array) => {

        if (val.isAddress == 1) {
          treeData.push({
            name: val.name,
            id: val.id,
            parentId: val.parentId,
            isChildren: val.children === undefined ? 0 : 1,
            icon: "add",
            children: this.readyTreeAddressData(val.children as Array<UserData.Organization>),
          });
        }

        if (val.isHasContact == 1) {
          treeData.push({
            name: val.name,
            id: val.id,
            parentId: val.parentId,
            isChildren: val.children === undefined ? 0 : 1,
            // icon: val.isOrganizationType == 1 ? <BankOutlined />:<HomeOutlined />,
            children: this.readyTreeAddressData(val.children as Array<UserData.Organization>),
          });
        }

        if (val.isContact == 1) {
          treeData.push({
            name: val.name,
            id: val.id,
            parentId: val.parentId,
            isChildren: val.children === undefined ? 0 : 1,
            icon: "add",
          });
        }
      });
    }
    return treeData;
  },
  /**
   * 预备Tree结构数据
   * @param data
   */
  // readyTreeData(data: AdminOrganizationData.Organization[]): any {
  //   const treeData = [];
  //   if (data) {
  //     data.forEach((val) => {
  //       if (val.isFolder === 1 && val.children === undefined) {
  //       } else {
  //         treeData.push({
  //           name: val.name,
  //           id: val.id,
  //           parentId: val.parentId,
  //           checkbox: true,
  //           isFolder: val.isFolder,
  //           isChildren: val.children === undefined ? 0 : 1,
  //           children: this.readyTreeData(val.children as AdminOrganizationData.Organization[]),
  //         });
  //       }
  //     });
  //   }
  //   return treeData;
  // },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const { globalData: { access, currentUser } } = getApp();
    this.setData({
      access,
      currentUser
    })
    this.loadTreePurpleList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    //刷新
    this.loadTreePurpleList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
* 生命周期函数--监听页面卸载
*/
  onUnload() {
    wx.setStorageSync('instruct-page', 'address')
    wx.setStorageSync('instruct-detail-index', 0)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onRefresh();
  },
  onRefresh() {
    //导航条加载动画
    wx.showNavigationBarLoading();
    //停止下拉刷新
    this.loadTreePurpleList()
    setTimeout(() => {
      wx.stopPullDownRefresh();
      wx.hideNavigationBarLoading();
    }, 1000);
  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})